const jwt=require('jsonwebtoken')

const auth = async (ctx, next) => {
    const { authorization } = ctx.request.header;
    console.log(ctx.request)
    if (authorization) {
        const token = authorization.split(' ')[1];
        
        try {
            jwt.verify(token, 'kkk')
            await next()
        } catch (error){
            ctx.status = 401
            ctx.body = {
                code: -1,
                message: '校验失败'+error.message
            }
        }

    } else {
        ctx.status = 401
        ctx.body = {
            code: -1,
            message: '无权限'
        }
    }
}

module.exports=auth